package controller;

import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.wiring.BundleWiring;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.openrdf.query.MalformedQueryException;
import org.openrdf.query.QueryEvaluationException;
import org.openrdf.repository.RepositoryException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.ui.ModelMap;

import database_interactions.SparqlEndpoint;
import model.RequestResultStorage;
import java.util.logging.Level;
/*
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
*/
import java.util.logging.Logger;

/**
 * BusinessServlet1
 * http://logging.apache.org/log4j/2.x/manual/configuration.html
 */

@Controller
@RequestMapping("/Views")
public class BusinessController1 {

	//static final Logger logger = LogManager.getLogger(BusinessController1.class.getName()); l4j
	 final static Logger LOGGER = Logger.getLogger(BusinessController1.class.getName()); 
	
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public BusinessController1() {
		super();
		LOGGER.setLevel(Level.INFO); 
	}
	/**
	 * Toujours passer par POST pour cette application
	 */
	@RequestMapping(value="/View1" ,method = RequestMethod.GET)
	public String  doGet(HttpServletRequest request, HttpServletRequest response, ModelMap map) throws ServletException, IOException {
		return doPost(request, response, map);
		
	}
	@RequestMapping(value="/View1" , method = RequestMethod.POST)
	public String doPost(HttpServletRequest request, HttpServletRequest response, ModelMap model) throws ServletException, IOException {
		//logger.debug("doPost");		
		//logger.debug("originalURI = "+request.getAttribute("originalURI"));
		LOGGER.info("Debut de doPost BusinessController1");
		LOGGER.info("originalURI = "+request.getAttribute("originalURI"));
		SparqlEndpoint mySparqleEndPoint = new SparqlEndpoint();
		/** Execution **/
		try {
			ArrayList<String> listToFillWithResults=new ArrayList<String>();
			mySparqleEndPoint.fillListWithResults(listToFillWithResults);
			RequestResultStorage myRequestStorage= new RequestResultStorage(listToFillWithResults);
			//prendre ce tableau de resultats et le transettre a la vue 

		} catch (RepositoryException | MalformedQueryException | QueryEvaluationException e) {
			e.printStackTrace();
		}
		ArrayList<String> liste=new ArrayList<String>();
		RequestResultStorage res=new RequestResultStorage(liste);
		model.put("resultat_vue_1",res);
		//logger.debug("RequestResultStorage.getTableauDeResultats().size() = "+res.getTableauDeResultats().size());
		LOGGER.info("RequestResultStorage.getTableauDeResultats().size() = "+res.getTableauDeResultats().size());
		LOGGER.info("Fin de doPost BusinessController1");
		return "View1";
	}
}
